<?php
#if(isset($_GET['id'])) {
	$s = "SESSION_TYPE, SUBJECT, CATALOG_NBR, CLASS_SECTION, CLASS_NBR, COURSE_TITLE_LONG, CREDITS, LOCATION, STND_MTG_PAT, STIME, ETIME, INSTRUCTORNAME, ENRL_TOT, ENRL_CAP, TERM, STRM, NOTETEXT, FACILITY_ID, SDATE, $order_locations AS locationorder, $order_locations_backwards AS locationback";

	$gt = "SELECT COUNT(DISTINCT CLASS_NBR) FROM Results$whereclause";
	#print "\n<!-- The query I am using to retrieve the total is '$gt' -->\n";
	if($gt = mssql_query($gt,$mslink)) {
		if($ttl = mssql_fetch_array($gt)) {
			$total = $ttl[0];
#			print "\n<!-- There are a total of $total records in the result set. -->\n";
		}
		mssql_free_result($gt);
	}
	
	$topclause = "SELECT DISTINCT TOP ".($start+$howmany).' '.$s." FROM Results$whereclause$orderby";
	#die($topclause);
	
	$retrievehm = (($start+$howmany) <= $total) ? $howmany : ($total-$start);
	
	$s = "SELECT * FROM (SELECT TOP $retrievehm ".str_replace('NOTETEXT','TRUNCNOTE',$s)." FROM (".str_replace(array('NOTETEXT','Cast(NOTETEXT as Varchar(8000)) AS TRUNCNOTE LIKE'),array('Cast(NOTETEXT as Varchar(8000)) AS TRUNCNOTE','NOTETEXT LIKE'),$topclause).") AS nt".str_replace(array('ASC','locationorder ASC'),array('DESC','locationback ASC'),$orderby).") AS nt2 $orderby";
	#die($s);
	
	#print "\n<!-- $s -->\n";
	
	unset($l);
	
	if($s = mssql_query($s,$mslink)) {
		#print "\n<!-- The query was successfully executed. $s -->\n";
		if($s !== TRUE && $s !== FALSE) {
			#print "\n<!-- The query returned some results. -->\n";
			$results = true;
			$i=0;
			$t=0;
			$tn = 0;
			ob_start();
			while($r = mssql_fetch_assoc($s)) {
				#print "\n<!-- We are looping through results -->\n";
				if(!isset($t) || $r['STRM'] != $t) {
					if(isset($l)) {
						echo '
			</tbody>
		</table>';
						unset($l);
					}
					$t = $r['STRM'];
					echo '
				<h2>Classes being offered during the <em>'.$terms[$t].'</em> academic term</h2>';
				}
				if(!isset($l) || $r['LOCATION'] != $l) {
					if(isset($l)) {
						echo '
			</tbody>
		</table>';
						$i=0;
					}
					$tn++;
					echo '
		<table id="'.strtolower($r['LOCATION']).'-'.$t.'" class="courselist';
					echo ($tn%2) ? ' maintable' : ' alttable';
					echo '">';
					echo '
			<caption>'.$course_locations[strtoupper($r['LOCATION'])].'</caption>';
					echo '
			<thead>
				<tr>
					<td class="course-checkbox" style="border-top: 1px solid #fff;border-left: 1px solid #fff;">&nbsp;</td>
					<th>Course Nbr.</th>
					<th>Subject, Section</th>
					<th>Title</th>
					<th>Credits</th>
					<th>Days</th>
					<th>Times</th>
					<th>Instructor</th>
					<th>Building</th>
					<th>Seats Left</th>
				</tr>
			</thead>
			<tbody>';
					$l = $r['LOCATION'];
				}
				$c = new sis_course($r);
				$notesql = "SELECT TOP 1 NOTETEXT FROM Results WHERE CLASS_NBR='".$c->class_nbr."' AND STRM='".$c->strm."'";
				if($notesql = mssql_query($notesql,$mslink)) {
					if($notesql !== TRUE && $notesql !== FALSE) {
						if($noters = mssql_fetch_assoc($notesql)) {
							$c->notetext = $noters['NOTETEXT'];
						}
						else {
							$c->notetext = NULL;
						}
					}
					else {
						$c->notetext = NULL;
					}
				}
				else {
					$c->notetext = NULL;
				}
				$c->formatvalues();
				$cd = new listedcourse;
				$cdinfo = trim($c->subject).' '.trim($c->catalog_nbr).'-'.trim($c->class_section);
				$cdinfo = $cd->findcourselinks($cdinfo);
				unset($cd);
				$coursetime = (!empty($c->stime)) ? $c->stime : '';
				$coursetime .= (!empty($c->stime) && !empty($c->etime)) ? ' to ' : '&nbsp;';
				$coursetime .= (!empty($c->etime)) ? $c->etime : '';
				$iname = explode(',',$c->instructorname);
				$c->instructorname = implode(', ',$iname);
				unset($iname);
				$c->class_section = trim($c->class_section);
				if(strtoupper($c->class_section[0]) == 'W') {
					if(emptyreturn(trim($c->stnd_mtg_pat))) {
						$c->stnd_mtg_pat = $GLOBALS['course_days']['VIRT'];
					}
					if(empty($c->stime) && empty($c->etime)) {
						$coursetime = $GLOBALS['course_days']['VIRT'];
					}
				}
				$c->facility_id = trim($c->facility_id);
				if(!empty($c->facility_id)) {
					$b = substr($c->facility_id,0,4);
					$bldg = (array_key_exists($b,$course_bldgs)) ? $course_bldgs[$b] : $c->facility_id;
				}
				else {
					$bldg = '&nbsp;';
				}
				echo '
				<tr class="row'.($i%2).'">
					<td class="course-checkbox"';
				echo (!empty($c->notetext)) ? ' rowspan="2"' : '';
				echo '><input type="checkbox" name="chosen-courses['.$c->class_nbr.']" id="chosen-courses_'.$c->class_nbr.'" /></td>
					<th scope="row"';
				echo (!empty($c->notetext)) ? ' rowspan="2"' : '';
				echo '>'.$c->class_nbr.'</th>
					<td>'.$cdinfo.'</td>
					<td>'.$c->course_title_long.'</td>
					<td>'.$c->credits.'</td>
					<td>'.$c->stnd_mtg_pat.'</td>
					<td>'.$coursetime/*.'</td>
					<td>'.formatDate($c->sdate,0)*/.'</td>
					<td>'.$c->instructorname.'</td>
					<td>'.$bldg.'</td>
					<td>';
				echo (($c->enrl_cap-$c->enrl_tot) > 0) ? ($c->enrl_cap-$c->enrl_tot) : 'Closed';
				echo '</td>';
				unset($cdinfo);
				unset($coursetime);
				if(!empty($c->notetext)) {
					$tmpnote = preg_replace('`([a-zA-Z0-9\!#$%&\'\*\+-/=\?\^_\`\{\|\}\~\.]+?)@([a-zA-Z0-9-\.]+?)\.([a-zA-Z0-9]{2,4})`i','<a href="mailto:$1@$2.$3">$1@$2.$3</a>',$c->notetext);
#					$tmpnote = preg_replace('`http://([^\s|\<]+?)`is','<a href="$1">$1</a>',$tmpnote);
					$tmpnote = make_clickable($tmpnote);
					echo '
				</tr>
				<tr class="row'.($i%2).' noterow">
					<th scope="row">Notes:</th>
					<td colspan="7">'.$tmpnote.'</td>';
					unset($tmpnote);
				}
				echo '
				</tr>';
				unset($c);
				$i++;
			}
			if(isset($l) && $l !== 0) {
				echo '
			</tbody>
		</table>';
			}
			$ctable = ob_get_contents();
			ob_end_clean();
		}
		elseif($s === true) {
			#print "<!-- The query returned no results. -->";
		}
		elseif($s === false) {
			#print "<!-- There was an error running the query. -->";
		}
		mssql_free_result($s);
		$prevlink = array();
		$nextlink = array();
		$qs = $_GET;
		unset($qs['pageinfo'],$qs['id']);
		$qs = json_encode($qs);

		if($start != 0) {
			$prevlink['start'] = ($start < $howmany) ? 'start=0' : 'start='.($start-$howmany);
			if($howmany != DEFHM) {
				$prevlink['howmany'] = 'howmany='.$howmany;
			}
			foreach($_GET as $k=>$v) {
				if($k != 'pageinfo' && $k != 'id') {
					$prevlink[$k] = (is_array($_GET[$k])) ? $k.'[]='.implode('&amp;'.$k.'[]=',$_GET[$k]) : $k.'='.$v;
				}
			}
		}
		if(($start+$howmany) <= $total) {
			$nextlink['start'] = 'start='.($start+$howmany);
			if($howmany != DEFHM) {
				$nextlink['howmany'] = 'howmany='.$howmany;
			}
			foreach($_GET as $k=>$v) {
				if($k != 'pageinfo' && $k != 'id') {
					if(is_array($_GET[$k])) {
						$nextlink[$k] = $k.'[]='.implode('&amp;'.$k.'[]=',$_GET[$k]);
					}
					else {
						$nextlink[$k] = $k.'='.$v;
					}
				}
			}
		}

		if($total > 0) {
			ob_start();
			require_once($GLOBALS['paths']['htmlpath'].'sis-courses/sis-pagination.inc.php');
			$pag = ob_get_contents();
			ob_end_clean();
		}
		else {
			$pag = '';
		}
		echo (!empty($pag)) ? '<div id="'.$hashname.'"><div class="toppagination">'.$pag.'<div class="cleared">&nbsp;</div></div>'.$ctable.'<div class="botpagination">'.$pag.'<div class="cleared">&nbsp;</div></div>'.((isset($qs)) ? '<div class="cacheparams">'.$qs.'</div>' : '').'</div>' : $ctable;
		unset($pag,$ctable);
	}
	mssql_close($mslink);
#}
?>
